package com.jpxx.netty.runtime;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ServerHandler extends ChannelHandlerAdapter{  
    
    @Override  
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  
    	 Request request = (Request) msg;  
         System.out.println("client发送的请求："+request);  
          
         //实例1个 然后写回客户端
         Response response = new Response();
         response.setId(request.getId());
         response.setResponseMessage("Message:"+request.getRequestMessage());
         response.setName("name:"+request.getName());  
         
         ctx.writeAndFlush(response);
         //.addListener(ChannelFutureListener.CLOSE);//冲刷并返回给客户端后，和客户端的通到断开
    }  
      
    @Override  
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  
        cause.printStackTrace();
        ctx.close();//通道关闭
    }  
  
}  
